/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package ly.count.ohos.demo;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ScrollView;
import ohos.global.configuration.Configuration;

import java.util.HashMap;
import java.util.Map;

import ly.count.ohos.sdk.Countly;

/**
 * 性能示例页面
 *
 * @since 2021-04-13
 */
public class AbilityExampleApm extends Ability {
    private static final int SUCCESS_CODE_100 = 100;
    private static final int SUCCESS_CODE_101 = 101;
    private static final int SUCCESS_CODE_200 = 200;
    private static final int SUCCESS_CODE_201 = 201;
    private static final int SUCCESS_CODE_202 = 202;
    private static final int SUCCESS_CODE_205 = 205;
    private static final int SUCCESS_CODE_300 = 300;
    private static final int SUCCESS_CODE_301 = 301;
    private static final int SUCCESS_CODE_303 = 303;
    private static final int SUCCESS_CODE_305 = 305;
    private static final int FAILURE_CODE_400 = 400;
    private static final int FAILURE_CODE_402 = 402;
    private static final int FAILURE_CODE_405 = 405;
    private static final int FAILURE_CODE_408 = 408;
    private static final int FAILURE_CODE_500 = 500;
    private static final int FAILURE_CODE_501 = 501;
    private static final int FAILURE_CODE_502 = 502;
    private static final int FAILURE_CODE_505 = 505;
    private static final String NETWORK_TRACE_KEY = "api/endpoint.1";
    int[] successCodes = new int[]{SUCCESS_CODE_100, SUCCESS_CODE_101, SUCCESS_CODE_200,
        SUCCESS_CODE_201, SUCCESS_CODE_202, SUCCESS_CODE_205,
        SUCCESS_CODE_300, SUCCESS_CODE_301, SUCCESS_CODE_303, SUCCESS_CODE_305};
    int[] failureCodes = new int[]{FAILURE_CODE_400, FAILURE_CODE_402, FAILURE_CODE_405,
        FAILURE_CODE_408, FAILURE_CODE_500, FAILURE_CODE_501,
        FAILURE_CODE_502, FAILURE_CODE_505};

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_example_apm);
        ScrollView sv = (ScrollView) findComponentById(ResourceTable.Id_sv);
        sv.enableScrollBar(ScrollView.AXIS_Y, true);
        findComponentById(ResourceTable.Id_button1).setClickedListener(this::onClickStartTrace1);
        findComponentById(ResourceTable.Id_button2).setClickedListener(this::onClickEndTrace1);
        findComponentById(ResourceTable.Id_button3).setClickedListener(this::onClickStartTrace2);
        findComponentById(ResourceTable.Id_button4).setClickedListener(this::onClickEndTrace2);
        findComponentById(ResourceTable.Id_button5).setClickedListener(this::onClickStartNetworkTrace1);
        findComponentById(ResourceTable.Id_button6).setClickedListener(this::onClickEndNetworkTrace1);
        findComponentById(ResourceTable.Id_button7).setClickedListener(this::onClickStartNetworkTrace2);
        findComponentById(ResourceTable.Id_button8).setClickedListener(this::onClickEndNetworkTrace2);
        Countly.sharedInstance().onStart(this);
    }

    @Override
    public void onStop() {
        Countly.sharedInstance().onStop();
        super.onStop();
    }

    @Override
    public void onConfigurationUpdated(Configuration configuration) {
        super.onConfigurationUpdated(configuration);
        Countly.sharedInstance().onConfigurationChanged(configuration);
    }

    private void onClickStartTrace1(Component v) {
        Countly.sharedInstance().apm().startTrace("Some_trace_key_1");
    }

    private void onClickStartTrace2(Component v) {
        Countly.sharedInstance().apm().startTrace("another key_1");
    }

    private void onClickEndTrace1(Component v) {
        Map<String, Integer> customMetric = new HashMap<>();
        final int abc = 1233;
        final int c44c = 1337;
        customMetric.put("ABC", abc);
        customMetric.put("C44C", c44c);
        Countly.sharedInstance().apm().endTrace("Some_trace_key_1", customMetric);
    }

    private void onClickEndTrace2(Component v) {
        Countly.sharedInstance().apm().endTrace("another key_1", null);
    }

    private void onClickStartNetworkTrace1(Component v) {
        Countly.sharedInstance().apm().startNetworkRequest(NETWORK_TRACE_KEY, "1337");
    }

    private void onClickEndNetworkTrace1(Component v) {
        // network trace of succeeding request
        final int bound = 700;
        final int plus = 200;

        int requestBytes = (int) (Math.random() * bound) + plus;
        int responseBytes = (int) (Math.random() * bound) + plus;
        int responseCode = successCodes[(int) (Math.random() * successCodes.length)];
        Countly.sharedInstance().apm().endNetworkRequest(NETWORK_TRACE_KEY,
            "1337", responseCode, requestBytes, responseBytes);
    }

    private void onClickStartNetworkTrace2(Component v) {
        Countly.sharedInstance().apm().startNetworkRequest(NETWORK_TRACE_KEY, "7331");
    }

    private void onClickEndNetworkTrace2(Component v) {
        // network trace of failing request
        final int bound = 700;
        final int plus = 250;

        int requestBytes = (int) (Math.random() * bound) + plus;
        int responseBytes = (int) (Math.random() * bound) + plus;
        int responseCode = failureCodes[(int) (Math.random() * failureCodes.length)];
        Countly.sharedInstance().apm().endNetworkRequest(NETWORK_TRACE_KEY,
            "7331", responseCode, requestBytes, responseBytes);
    }
}
